<?php

namespace App\Http\Controllers\Ajax;

use App\Models\CreditsExchangeRecord;
use App\Models\CreditsExchangeRule;
use App\Repositories\OrderRepository;
use Illuminate\Http\Request;

class AboutOrderController extends Controller
{
    /**
     * 积分兑换
     * @param Request $request
     * @return json
     */
    public function creditsExchange(Request $request)
    {
        $id_credits_exchange_rule = $request->id;

        //查找兑换的奖品
        $exchange_rule = CreditsExchangeRule::where('id_credits_exchange_rule',$id_credits_exchange_rule)->with('merchantTicket')->first();

        if(empty($exchange_rule)) return $this->response->array(['code'=>404,'msg'=>'不好意思，未找到礼品信息']);

        if($exchange_rule->id_merchant_ticket && !$exchange_rule->merchantTicket) return $this->response->array(['code'=>404,'msg'=>'不好意思，该奖品已被清除']);
        //查找记录被领取的条数
        $recode_exchange_sum = CreditsExchangeRecord::where('id_credits_exchange_rule',$id_credits_exchange_rule)->count('*');

        $exchange_rule_arr = $exchange_rule->toArray();

        if($recode_exchange_sum*1 >= $exchange_rule_arr['goods_count']) return $this->response->array(['code'=>403,'msg'=>'不好意思，库存不足']);

        //查找会员卡余积分信息
        $mcard_info = $this->getSeesionWxuseMcard(true);

        if($mcard_info->remain_credits*1 < $exchange_rule_arr['credits']*1)
            return $this->response->array(['code'=>403,'msg'=>'不好意思，您的积分不足']);

        $img = '';
        if(isset($exchange_rule_arr['merchant_ticket']['ticket_shading'])){
            $img = $exchange_rule_arr['merchant_ticket']['ticket_shading'].'|'.$exchange_rule_arr['merchant_ticket']['ticket_bgcolor'];
        }else{
            $img = $exchange_rule_arr['goods_image'];
        }

        //插入数据
        $res = CreditsExchangeRecord::create([
            'id_wxuser_mcard'=>$mcard_info->id_wxuser_mcard,
            'id_credits_exchange_rule'=>$id_credits_exchange_rule,
            'id_merchant_ticket'=>$exchange_rule_arr['id_merchant_ticket'],
            'id_merchant'=>$exchange_rule_arr['id_merchant'],
            'value'=>-$exchange_rule_arr['credits'],
            'title'=>isset($exchange_rule_arr['merchant_ticket']['name'])? $exchange_rule_arr['merchant_ticket']['name']: $exchange_rule_arr['goods_name'],
            'goods_img'=>$img,
            'desc'=>isset($exchange_rule_arr['merchant_ticket']['specification'])? $exchange_rule_arr['merchant_ticket']['specification']: $exchange_rule_arr['goods_description'],
            'proof_expiry_time'=>now()->addDays($exchange_rule_arr['proof_expiry_days'])->toDateTimeString(),
            'current_credits'=>$mcard_info->remain_credits,
        ]);

        if($res) {

            //超出这修改状态为over
            if($recode_exchange_sum*1+1 >= $exchange_rule_arr['goods_count']){
                $exchange_rule->update(['status'=>'over']);
            }

            return $this->response->array(['code'=>200,'msg'=>'兑换成功']);
        }

        return $this->response->array(['code'=>500,'msg'=>'不好意思，兑换失败']);
    }

    /**
     * 绑定礼品卡
     * @param Request $request
     * @param OrderRepository $repository
     * @return mixed
     */
    public function bindGiftCard(Request $request,OrderRepository $repository)
    {
        $gift_no = $request->gift_no;
        $card_password = $request->card_password;

        if(!$gift_no || !$card_password) return $this->response->array(['code'=>402,'msg'=>'请填写卡号或密码']);

        $gift_card = $repository->getGiftBuyUsedByGiftNo($gift_no);

        if(empty($gift_card)) return $this->response->array(['code'=>404,'msg'=>'未找到礼品卡信息']);

        if(!empty($gift_card->use_user_id)) return $this->response->array(['code'=>203,'msg'=>'该礼品卡已被绑定']);

        if($card_password !=$gift_card->card_password ) return $this->response->array(['code'=>403,'msg'=>'密码错误']);

        $wxuser_mcard = $this->getSeesionWxuseMcard();
        $res = $gift_card->update([
            'use_id_wxuser_mcard'=>$wxuser_mcard->id_wxuser_mcard,
            'use_wxuser_mcard_name'=>$wxuser_mcard->member_optional_name,
            'use_telephone'=>$wxuser_mcard->member_optional_telephone,
            'use_date_time'=>now()->toDateTimeString(),
        ]);

        if($res) return $this->response->array(['code'=>200,'msg'=>'恭喜，绑定成功']);

        return $this->response->array(['code'=>500,'msg'=>'不好意思，绑定失败']);
    }

}
